<?php
require_once 'classes/data/Photo.php';
require_once 'classes/DB.php';
require_once 'config.php';

/**
 * This class represents a category in the photoalbum system.
 */
class Category
{
	/**
	 * Creates an instance of the class.
	 */
	public function __construct() /*Category*/
	{
	}

	private $categoryId; /*int*/

	/**
	 * Gets the ID of the category.
	 */
	public function getCategoryId() /*int*/
	{
		return $this->categoryId;
	}

	/**
	 * Sets the ID of the category.
	 *
	 * @param int $categoryId
	 * 		  The new ID of the category.
	 */
	public function setCategoryId(/*int*/ $categoryId)
	{
		$this->categoryId = $categoryId;
	}

	private $name; /*string*/

	/**
	 * Gets the name of the category.
	 */
	public function getName() /*string*/
	{
		return $this->name;
	}

	/**
	 * Sets the name of the category.
	 *
	 * @param string $name
	 * 		  The new name of the category.
	 */
	public function setName(/*string*/ $name)
	{
		$this->name = $name;
	}

	private $userId; /*int*/

	/**
	 * Gets the user ID of the user who created this category.
	 */
	public function getUserId() /*int*/
	{
		return $this->userId;
	}

	/**
	 * Sets the user ID of the user who created this category.
	 *
	 * @param int $userId
	 * 		  The new user ID of the user.
	 */
	public function setUserId(/*int*/ $userId)
	{
		$this->userId = $userId;
	}

	private $parentId; /*int*/

	/**
	 * Gets the ID of the parent category.
	 */
	public function getParentId() /*int*/
	{
		return $this->parentId;
	}

	/**
	 * Sets the parent category ID of the category.
	 *
	 * @param int $parentId
	 * 		  The new ID of the parent category of the category.
	 */
	public function setParentId(/*int*/ $parentId)
	{
		$this->parentId = $parentId;
	}

	/**
	 * Gets the child categories of this category.
	 *
	 * @return
	 * 		  An array of the child categories.
	 */
	public function getCategories() /*array*/
	{
		return $_SESSION['category']->getCategoriesByParent($this->getCategoryId());;
	}
	
	/**
	 * Gets the child categories of this category of a specific parent
	 *
	 * @return
	 * 		  An array of the child categories.
	 */
	static public function getCategoriesByParent( $parentId ) /*array*/
	{
		/*resource*/ $result = DB::getInstance()->executeProcedure('get_categories',
				array(DB::escapeInt($parentId)));
		/*array*/ $categories = array();
		while ($result)
		{
			/*array*/ $categoryData = DB::getInstance()->getNextRow($result);
			if (!isset($categoryData) || !isset($categoryData['category_id']))
			{
				break;
			}
			
			/*Category*/ $category = new Category(DB::getInstance());
			$category->setCategoryId($categoryData['category_id']);
			$category->setName($categoryData['name']);
			$category->setUserId($categoryData['user_id']);
			$category->setParentId($parentId);
			array_push($categories, $category);
		}
		return $categories;
	}

	/**
	 * Gets the child photos of this category.
	 *
	 * @return
	 * 		  An array of the child photos.
	 */
	public function getPhotos() /*array*/
	{
		return Category::getPhotosByCategory($this->getCategoryId());
	}
	
	/**
	 * Gets the child photos of category.
	 *
	 * @return
	 * 		  An array of the child photos.
	 */
	static public function getPhotosByCategory($categoryId) /*array*/
	{
		/*resource*/ $result = DB::getInstance()->executeProcedure('get_photos',
				array(DB::escapeInt($categoryId)));
		/*array*/ $photos = array();
		while ($result)
		{
			/*array*/ $photoData = DB::getInstance()->getNextRow($result);
			if (!isset($photoData) || !isset($photoData['photo_id']))
			{
				break;
			}
			
			/*Photo*/ $photo = new Photo(DB::getInstance());
			$photo->setPhotoId($photoData['photo_id']);
			$photo->setCreationTime($photoData['creation_date']);
			$photo->setName($photoData['name']);
			$photo->setDescription($photoData['description']);
			$photo->setCategoryId($categoryId);
			array_push($photos, $photo);
		}
		return $photos;
	}

	/**
	 * Adds a category to this category.
	 *
	 * @param Category $category
	 * 		  The category to add.
	 */
	public function addCategory(/*Category*/ $category)
	{
		$categories = $_SESSION['category']->getCategories();
		foreach ($categories as $c)
		{
			if($c->getName() == $category->getName())
			{
				return "Category already exists";
			}
		}
		if($category->getName() == "")
		{
			return "Category name is empty";
		}
		$result = DB::getInstance()->executeProcedure('create_category',
				array(DB::escapeString($category->getName()),
					  DB::escapeInt($this->getCategoryId())),
				array('category_id'));
		/*array*/ $categoryData = DB::getInstance()->getNextRow($result);
		$category->setCategoryId($categoryData['category_id']);
		$category->setParentId($this->getCategoryId());
		return "";
	}

	/**
	 * Adds a photo to this category.
	 *
	 * @param Photo $photo
	 * 		  The photo to add.
	 * 
	 * @param string $temporaryFilename
	 * 		  The filename of the temporary file holding the photo data.
	 */
	public function addPhoto(/*Photo*/ $photo, /*string*/ $temporaryFilename)
	{
		if($photo->getName() == "")
		{
			return "Photo name is empty";
		}
		if($temporaryFilename == "")
		{
			return "Photo file name is empty";
		}
		$result = DB::getInstance()->executeProcedure('create_photo',
				array(DB::escapeString($photo->getName()),
					  DB::escapeString($photo->getDescription()),
					  DB::escapeInt($this->getCategoryId())),
				array('photo_id'));
		/*array*/ $photoData = DB::getInstance()->getNextRow($result);
		$photo->setPhotoId($photoData['photo_id']);
		$photo->setCategoryId($this->getCategoryId());
		move_uploaded_file($temporaryFilename, ROOT_DIR . DIR_DELIM . $photo->getFilename());
		return "";
	}

	/**
	 * Edits the category using its properties.
	 */
	public function edit()
	{
		$categories = $_SESSION['category']->getCategoriesByParent($this->getParentId());
		foreach ($categories as $c)
		{
			if($c->getName() == $this->getName())
			{
				return "Category already exists";
			}
		}
		if($this->getName() == "")
		{
			return "Category name is empty";
		}
		DB::getInstance()->executeProcedure('edit_category',
				array(DB::escapeInt($this->getCategoryId()),
					  DB::escapeString($this->getName())));
	}

	/**
	 * Deletes the category using its properties.
	 */
	public function delete()
	{
		DB::getInstance()->executeProcedure('delete_category',
				array(DB::escapeInt($this->getCategoryId())));
		/*array*/ $photos = glob(ROOT_DIR . DIR_DELIM . FILES_DIR . $this->getCategoryId() . FILE_DELIM  . '*');
		foreach ($photos as $photo)
		{
			unlink($photo);
		}
	}
	
	/**
	 * Loads a category using its properties.
	 */
	public function load()
	{
		/*resource*/ $result = DB::getInstance()->executeProcedure('get_category',
				array(DB::escapeInt($this->getCategoryId())),
				array('name', 'user_id', 'parent_id'));
		/*array*/ $categoryData = DB::getInstance()->getNextRow($result);
		$this->setName($categoryData['name']);
		$this->setUserId($categoryData['user_id']);
		$this->setParentId($categoryData['parent_id']);
	}
} // end class
?>